package MysingleListPractice_Once_time;

public class Test {
    public static void main11(String[] args) {
        MysingleList mysingleList=new MysingleList();
        mysingleList.addLast(32);
        mysingleList.addLast(148);
        mysingleList.addLast(45);
        mysingleList.addLast(12);

        MysingleList.Node ret=mysingleList.middleNode();
        mysingleList.display(ret);

    }
    //合并两个有序链表
    public MysingleList.Node mergeTwoLists(MysingleList.Node head1, MysingleList.Node head2) {
        MysingleList.Node newhead=new MysingleList.Node(0);
        MysingleList.Node tmp=newhead;
        while(head1!=null&&head2!=null){
            if (head1.val > head2.val) {
                tmp.next=head2;
                head2=head2.next;
                tmp=tmp.next;
            }else {
                tmp.next=head1;
                head1=head1.next;
                tmp=tmp.next;
            }
        }

        if (head1!=null){
            tmp.next=head1;
        } else if (head2!=null) {
            tmp.next=head2;
        }
        return newhead.next;
    }
    //判断两个链表是否香蕉
    // Y型，先计算两个链表的差值，让长的先走差值步，然后一起往后走
    public MysingleList.Node getIntersectionNode(MysingleList.Node headA, MysingleList.Node headB) {
        MysingleList.Node plong=headA;
        MysingleList.Node pshort=headB;
        int lenA=0;
        int lenB=0;
        while (plong!=null){
            plong=plong.next;
            lenA++;
        }
        while (pshort!=null){
            pshort=pshort.next;
            lenB++;
        }
        plong=headA;
        pshort=headB;
        int len=lenA-lenB;
        if (len<0){//说明A短
            pshort=headA;
            plong=headB;
        }
        //计算长度完成
        //长的先走Len步
        while (len!=0){
            plong=plong.next;
        }
        //两个一起走
        while (plong!=pshort){
            plong=plong.next;
            pshort=pshort.next;
        }
        //没相交(都是null)或者有相交界点
        return pshort;
    }


    public static void main(String[] args) {
        MysingleList mysingleList=new MysingleList();
        mysingleList.addLast(14);
        mysingleList.addLast(17);
        mysingleList.addLast(28);
        mysingleList.addLast(5);
        mysingleList.addLast(9);
        MysingleList.Node ret=mysingleList.partition(10);
        mysingleList.display(ret);

    }
}
